package traceMyItems.business;

import java.util.*;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.apache.commons.lang.NotImplementedException;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable
public class Workflow {
	private static final Logger log = Logger.getLogger(Workflow.class.getName());
	
	@Persistent
	private StatusNode objStartNode;
	@Persistent
	private List<StatusNode> lstStatusNodes;
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key=null;
	
	
	public void createKey() {
        //this.key = KeyFactory.createKey(User.class.getSimpleName(), strUsername+":"+strEmail);
		///TODO: Aun queda crear criterio de creacion de clave para esta clase
		this.key = null;
    }
	
	public Key getKey()
	{
		return key;
	}
	
	public Workflow(StatusNode objStartNodeParam)
	{
		lstStatusNodes = new ArrayList<StatusNode>();
		objStartNode = objStartNodeParam;
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(objStartNode);
        } finally {
            pm.close();
        }
		
		lstStatusNodes.add(objStartNodeParam);
		
	
	        try {
	            pm.makePersistent(this);
	        } finally {
	            pm.close();
	        }
	}

	public Workflow()
	{
		lstStatusNodes = new ArrayList<StatusNode>();
		StatusNode objStartNode = new StatusNode();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(objStartNode);
        } finally {
            pm.close();
        }
		
		lstStatusNodes.add(objStartNode);
		
		
	        try {
	            pm.makePersistent(this);
	        } finally {
	            pm.close();
	        }
	}
	
	public List<StatusType> getNextStatusType(StatusType objCurrentStatusType) throws NotImplementedException
	{
		for(StatusNode objStatusNode : lstStatusNodes)
		{
			if(objStatusNode.getStatusType()==objCurrentStatusType)
			{
				return objStatusNode.getNextStatus();
			}
		}
		return null;
	}

	public StatusNode getStartNode() {
		return objStartNode;
	}

	public void setStartNode(StatusNode objStartNodeParam) {

		objStartNode = objStartNodeParam;
	}
	
	
}
